package com.huilink.webmail.util.client.widgets;

import com.google.gwt.i18n.client.ConstantsWithLookup;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.KeyboardListener;

/**
 * A simple example of an 'about' dialog box.
 */
public class HelpPanel extends HelpBox {
	
//  private static HelpContent constants = (HelpContent)GWT.create(HelpContent.class);
  
//  public HelpPanel(String helpKey) {
//      this(helpKey, null, null, null, null, null);
//  }
  
	public HelpPanel(ConstantsWithLookup constants, ButtonSettings settings) {
      super(settings.getStyleBox(), settings.getStyleCaption(), settings.getStyleCaptionText());
      // Use this opportunity to set the dialog's caption.
      setText("Help info");
      // Create a DockPanel to contain the 'about' label and the 'OK' button.
      DockPanel outer = new DockPanel();
      outer.setWidth("500px");
      outer.setSpacing(4);

      // Create the 'about' label. Placing it in the 'rest' position within the
      // dock causes it to take up any remaining space after the 'OK' button
      // has been laid out.
      HTML text = new HTML(helpContentConvert(constants.getString(settings.getHelpKey())));
      String styleContentText = settings.getStyleContentText();
      if (styleContentText == null){
          text.setStyleName("helpContent");
      } else {
          text.setStyleName(styleContentText);
      }
      outer.add(text, DockPanel.CENTER);

      // Add a bit of spacing and margin to the dock to keep the components from
      // being placed too closely together.
      outer.setSpacing(8);
      String closeBtn = settings.getCloseBtnFile();
      if (closeBtn == null) {
          closeBtn = "help_close.gif";
      }
      
      setCloseButton(new ImageButton(closeBtn,closeBtn, 16, 16));
      setWidget(outer);
  }



	public static String helpContentConvert(String orig) {
		return orig.replaceAll("\\r\\n", "<br/>");
	}
	
  public boolean onKeyDownPreview(char key, int modifiers) {
    // Use the popup's key preview hooks to close the dialog when either
    // enter or escape is pressed.
    switch (key) {
      case KeyboardListener.KEY_ENTER:
      case KeyboardListener.KEY_ESCAPE:
        hide();
        break;
      default:
          break;
    }

    return true;
  }

}
